package com.zijie.hosptal.config;

import com.zijie.hosptal.service.AiCodeHelperService;
import com.zijie.hosptal.tools.InterviewQuestionTool;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCodeHelperServiceFactory {

    private ChatModel qwenChatModel;//qwen大模型
    private ContentRetriever ragService;//RAG检索
    private StreamingChatModel qwenStreamingChatModel;//流式输出
    private InterviewQuestionTool interviewQuestionTool;

    @Autowired
    public void setInterviewQuestionTool(InterviewQuestionTool interviewQuestionTool) {
        this.interviewQuestionTool = interviewQuestionTool;
    }

    @Autowired
    public void setQwenChatModel(ChatModel qwenChatModel) {
        this.qwenChatModel = qwenChatModel;
    }

    @Autowired
    public void setRagService(ContentRetriever ragService) {
        this.ragService = ragService;
    }

    @Autowired
    public void setQwenStreamingChatModel(StreamingChatModel qwenStreamingChatModel) {
        this.qwenStreamingChatModel = qwenStreamingChatModel;
    }

    @Bean
    public AiCodeHelperService aiCodeHelperService() {
        //会话记忆----最多存储10条信息
        ChatMemory chatMemory= MessageWindowChatMemory.withMaxMessages(10);
        AiCodeHelperService aiCodeHelperService = AiServices.builder(AiCodeHelperService.class)
                .chatModel(qwenChatModel)
                .streamingChatModel(qwenStreamingChatModel)//流式输出
                .chatMemory(chatMemory)//会话记忆
                .chatMemoryProvider(memoryId->MessageWindowChatMemory.withMaxMessages(10))//每个聊天独立对应id
                .contentRetriever(ragService)//rag检索生成
                .tools(interviewQuestionTool)//工具
                .build();
        return aiCodeHelperService;
    }
}
